package com.zhongkai.service.impl;

import com.mysql.jdbc.StringUtils;
import com.zhongkai.Bean.PageBean;
import com.zhongkai.app.service.PageQuerDao;
import com.zhongkai.app.service.SendDateOrUnlikeService;
import com.zhongkai.model.utils.ZhongkaiResult;
import com.zhongkai.model3.SendDate;
import com.zhongkai.model3.Unlike;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class SendDateOrUnlikeServiceImpl implements SendDateOrUnlikeService {
    @Autowired
    private PageQuerDao pageQuerDao;


    /**
     * 派单记录列表
     *
     * @param page 页数
     * @param rows 每页数据数量
     */

    @Override
    public ZhongkaiResult sendDateList(Integer page, Integer rows) {
        if (page == 0 || rows == 0) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        try {
            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(page);
            pageBean.setPageSize(rows);
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(SendDate.class);
            detachedCriteria.addOrder(Order.desc("sendTime")); //根据传入的字段进行降序排序
            pageBean.setDetachedCriteria(detachedCriteria);
            pageQuerDao.pageQuery(pageBean);
            return ZhongkaiResult.ok(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(405, "查询失败");
        }

    }


    /**
     * 异常记录列表
     *
     * @param page 页数
     * @param rows 每页数据数量
     */
    @Override
    public ZhongkaiResult unlikeList(Integer page, Integer rows) {
        if (page == 0 || rows == 0) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        try {
            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(page);
            pageBean.setPageSize(rows);
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Unlike.class);
            detachedCriteria.addOrder(Order.desc("solveTime")); //根据传入的字段进行降序排序
            pageBean.setDetachedCriteria(detachedCriteria);
            pageQuerDao.pageQuery(pageBean);
            return ZhongkaiResult.ok(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(405, "查询失败");
        }

    }

    /**
     * 派单记录列表查询
     *
     * @param name 维修人员姓名
     * @param page 页数
     * @param rows 每页数据数量
     * @param time 时间
     */
    @Override
    public ZhongkaiResult seekList(String name, Integer page, Integer rows, Long time) {
        if (page == 0 || rows == 0 ) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        try {
            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(page);
            pageBean.setPageSize(rows);
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(SendDate.class);

            if (!StringUtils.isNullOrEmpty(name)) {
                detachedCriteria.add(Restrictions.eq("resolveName", name)); //增加条件
            }

            if (time != 0) {
                DateTime now = new DateTime(time);
                long millis = now.dayOfWeek().roundCeilingCopy().getMillis();//明天00点的时间戳
                long floor = now.dayOfWeek().roundFloorCopy().getMillis();//今天00点的时间戳

                detachedCriteria.add(Restrictions.gt("sendTime", floor)); //增加条件
                detachedCriteria.add(Restrictions.lt("sendTime", millis)); //增加条件
            }

            detachedCriteria.addOrder(Order.desc("sendTime")); //根据传入的字段进行降序排序
            pageBean.setDetachedCriteria(detachedCriteria);
            pageQuerDao.pageQuery(pageBean);
            return ZhongkaiResult.ok(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(405, "查询失败");
        }


    }


    /**
     * 异常记录列表查询
     *
     * @param name 用户姓名
     * @param page 页数
     * @param rows 每页数据数量
     * @param time 时间
     */
    @Override
    public ZhongkaiResult unitKe(String name, Integer page, Integer rows, Long time) {
        if (page == 0 || rows == 0 ) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        try {
            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(page);
            pageBean.setPageSize(rows);
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Unlike.class);

            if (!StringUtils.isNullOrEmpty(name)) {
                detachedCriteria.add(Restrictions.eq("userName", name)); //增加条件
            }

            if (time != 0) {
                DateTime now = new DateTime(time);
                long millis = now.dayOfWeek().roundCeilingCopy().getMillis();//明天00点的时间戳
                long floor = now.dayOfWeek().roundFloorCopy().getMillis();//今天00点的时间戳

                detachedCriteria.add(Restrictions.gt("solveTime", floor)); //增加条件
                detachedCriteria.add(Restrictions.lt("solveTime", millis)); //增加条件
            }

            detachedCriteria.addOrder(Order.desc("solveTime")); //根据传入的字段进行降序排序

            pageBean.setDetachedCriteria(detachedCriteria);
            pageQuerDao.pageQuery(pageBean);
            return ZhongkaiResult.ok(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(405, "查询失败");
        }
    }




}
